package eai.tp2.lpco.web.servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import eai.tp2.lpco.database.DigitalCamera;
import eai.tp2.lpco.web.beans.Camera;
import eai.tp2.lpco.web.beans.ErrorList;
import eai.tp2.lpco.web.beans.ResultsBean;
import eai.tp2.lpco.wsclients.camerasupplier.ICameraSupplierClient;

/**
 * Servlet implementation class ExternalCatalog
 */
public class ExternalCatalog extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ExternalCatalog() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		String action = request.getParameter("do");
		if(action.equals("detail")) {
			String name = request.getParameter("name");
			ResultsBean results = (ResultsBean) session.getAttribute("results");
			DigitalCamera temp = results.getResult(name);
			if(temp != null)
			{
				Camera cam = new Camera();
				cam.setBrandName(temp.getBrandName());
				cam.setModelName(temp.getModelName());
				cam.setDate(temp.getDate());
				cam.setCameraId(temp.getCameraId());
				cam.setDescription(temp.getDescription());
				cam.setEffectivePixels(temp.getEffectivePixels());
				cam.setImageLink(temp.getImageLink());
				cam.setIsoRating(temp.getIsoRating());
				cam.setImageRatio(temp.getImageRatio());
				cam.setMaxRes(temp.getMaxRes());
				cam.setMinRes(temp.getMinRes());
				cam.setMaxShutter(temp.getMaxShutter());
				cam.setMinShutter(temp.getMinShutter());
				cam.setReviewLink(temp.getReviewLink());
				cam.setSensorSize(temp.getSensorSize());
				cam.setPrice(temp.getPrice());
				request.setAttribute("camera", cam);
				RequestDispatcher rd = request.getRequestDispatcher("result_camera_details.jsp");
				rd.forward(request, response);	
			} else {
				String error;
				error = "The camera '"+name+"' doesn't exist.";
				List<String> errors = new ArrayList<String>();
				errors.add(error);
				ErrorList list = new ErrorList();
				list.setErrorList(errors);		
				request.setAttribute("list", list);
				RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
				rd.forward(request, response);	
			}
		} else {
			//quer dizer que alguém andou a editar o link no browser 
			//error message
			String error;
			error = "Action not recognized.";
			List<String> errors = new ArrayList<String>();
			errors.add(error);
			ErrorList list = new ErrorList();
			list.setErrorList(errors);		
			request.setAttribute("list", list);
			RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
			rd.forward(request, response);	
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		ICameraSupplierClient supplierClient = null;
		InitialContext ctx = null;
		try {
			ctx = new InitialContext();
			supplierClient = (ICameraSupplierClient) ctx.lookup("CameraSupplierClientBean/remote");
		} catch (NamingException e) {			
			String error;
			error = "Error while lookpu for CameraSupplierClientBean: \n"+e.getMessage();
			List<String> errors = new ArrayList<String>();
			errors.add(error);
			ErrorList list = new ErrorList();
			list.setErrorList(errors);		
			request.setAttribute("list", list);
			RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
			rd.forward(request, response);	
		}		
		String query = request.getParameter("search");
		List<DigitalCamera> cameras = supplierClient.executeQuery(query);
		ResultsBean results = new ResultsBean();
		results.setQuery(query);
		results.setResults(cameras);
		session.setAttribute("results", results);
		response.sendRedirect("search.jsp");
	}

}
